package interfaces.menu.joinGame;

import main.InitGame;
import networking.GameInfoCreator;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.FocusEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.layout.StaticLayout;

import com.jme.system.DisplaySystem;

import fileHandling.language.interfaces.JoinGameText;
import fileHandling.language.interfaces.MenuText;
import interfaces.GUISource;
import interfaces.inputValidation.IPInputChecker;
import interfaces.inputValidation.InputChecker;
import interfaces.language.LanguageButton;
import interfaces.language.LanguageLabel;
import interfaces.language.LanguageWindow;
import interfaces.menu.AbsMenu;
import interfaces.menu.joinGame.gamesTable.GamesTable;
import interfaces.menu.joinGame.gamesTable.GamesTableWidget;
import interfaces.options.TextEditorOptionWidget;
import interfaces.options.TextInputError;
import interfaces.options.TextInputErrorWindow;

public class IPWindow extends LanguageWindow {
	
	private TextEditorOptionWidget ipEditor;
	private LanguageButton joinButton, statusButton;
	private int widgetHeight;
	private ImageFont font;
	private GamesTable table;
	private GameInfoCreator infoCreator;
	private AbsMenu menu;
	
	public IPWindow(AbsMenu menu, GamesTable table) {
		super(true, JoinGameText.Connect_to_IP);
		this.table = table;
		this.menu = menu;
		
		infoCreator = new GameInfoCreator();
		
		content.setLayoutManager(new StaticLayout());
		
		font = GUISource.middleFont;
		
		DisplaySystem display = DisplaySystem.getDisplaySystem();
		
		widgetHeight = font.getHeight() / 3 * 4;
		int height = widgetHeight * 4;
		int width = display.getWidth() / 3;
		int x = (display.getWidth() / 2) - (width / 2);
		int y = (display.getHeight() / 2) - (height / 2);
		
		setXY(x, y);
		setSize(width, height);
		layout();
		
		createIPEditor();
		createButtons();
		
		layout();
	}
	
	private void createIPEditor() {
		LanguageLabel ipLabel = new LanguageLabel(JoinGameText.IP, font);
		ipLabel.setXY(0, widgetHeight);
		ipLabel.setSize(content.getWidth() / 4, widgetHeight);
		content.addWidget(ipLabel);
		
		InputChecker ipChecker = new IPInputChecker();
		ipEditor = new TextEditorOptionWidget("", title, ipChecker);
		ipEditor.setXY(ipLabel.getWidth(), widgetHeight);
		ipEditor.setSize(content.getWidth() - ipLabel.getWidth(), widgetHeight);
		content.addWidget(ipEditor);
	}
	
	private void createButtons() {
		int buttonWidth = content.getWidth() / 2;
		
		joinButton = new LanguageButton(MenuText.Join_Game, font);
		joinButton.setSize(buttonWidth, widgetHeight);
		joinButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				if(!inputIsValid()) return;
				
				GamesTableWidget widget = getTableEntry();
				if(widget != null) {
					InitGame.get().initNewGame(false, widget.getGameCycle());
				} else {
					System.out.println("Unable to connect to " + ipEditor.getValue());
				}
			}
		});
		content.addWidget(joinButton);
		
		statusButton = new LanguageButton(JoinGameText.Status, font);
		statusButton.setSize(buttonWidth, widgetHeight);
		statusButton.setX(buttonWidth);
		statusButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				if(!inputIsValid()) return;
				
				GamesTableWidget widget = getTableEntry();
				if(widget != null) {
					table.addGame(widget);
					close();
				} else {
					System.out.println("Unable to connect to " + ipEditor.getValue());
				}
			}
		});
		content.addWidget(statusButton);
	}
	
	private boolean inputIsValid() {
		String errorText = ipEditor.checkInput();
		if(errorText != null) {
			TextInputError error = new TextInputError(ipEditor, errorText);
			TextInputErrorWindow window = new TextInputErrorWindow(error, menu);
			menu.addWidget(window);
			return false;
		}
		return true;
	}
	
	private GamesTableWidget getTableEntry() {
		return infoCreator.createWidget(ipEditor.getValue());
	}
	
	@Override
	public void focusChanged(FocusEvent event) {
		super.focusChanged(event);
		ipEditor.focusChanged(event);
	}
}